/*******************************************************************************
  MPLAB Harmony Application Source File
  
  Company:
    Microchip Technology Inc.
  
  File Name:
    app1.c

  Summary:
    This file contains the source code for the MPLAB Harmony application.

  Description:
    This file contains the source code for the MPLAB Harmony application.  It 
    implements the logic of the application's state machine and it may call 
    API routines of other MPLAB Harmony modules in the system, such as drivers,
    system services, and middleware.  However, it does not call any of the
    system interfaces (such as the "Initialize" and "Tasks" functions) of any of
    the modules in the system or make any assumptions about when those functions
    are called.  That is the responsibility of the configuration-specific system
    files.
 *******************************************************************************/

// DOM-IGNORE-BEGIN
/*******************************************************************************
Copyright (c) 2013-2014 released Microchip Technology Inc.  All rights reserved.

Microchip licenses to you the right to use, modify, copy and distribute
Software only when embedded on a Microchip microcontroller or digital signal
controller that is integrated into your product or third party product
(pursuant to the sublicense terms in the accompanying license agreement).

You should refer to the license agreement accompanying this Software for
additional information regarding your rights and obligations.

SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER
CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR
OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR
CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF
SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
(INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
 *******************************************************************************/
// DOM-IGNORE-END


// *****************************************************************************
// *****************************************************************************
// Section: Included Files 
// *****************************************************************************
// *****************************************************************************

#include "app1.h"
#include "pcf8575.h"
#include "user_config.h"

// *****************************************************************************
// *****************************************************************************
// Section: Global Data Definitions
// *****************************************************************************
// *****************************************************************************

// *****************************************************************************
/* Application Data

  Summary:
    Holds application data

  Description:
    This structure holds the application's data.

  Remarks:
    This structure should be initialized by the APP_Initialize function.
    
    Application strings and buffers are be defined outside this structure.
*/

// *****************************************************************************
/* Application Data

  Summary:
    Holds application data

  Description:
    This structure holds the application's data.

  Remarks:
    Application strings and buffers are be defined outside this structure.
 */

APP1_DATA app1Data;



// *****************************************************************************
// *****************************************************************************
// Section: Application Callback Functions
// *****************************************************************************
// *****************************************************************************

/* TODO:  Add any necessary callback funtions.
*/

// *****************************************************************************
// *****************************************************************************
// Section: Application Local Functions
// *****************************************************************************
// *****************************************************************************

/* TODO:  Add any necessary local functions.
*/


// *****************************************************************************
// *****************************************************************************
// Section: Application Initialization and State Machine Functions
// *****************************************************************************
// *****************************************************************************

void get_well_state(void){

    uint16_t well_run_state = (mes_tbl[MES_OFS_DI_STATE] & 0x0003);

    switch (well_run_state) {
        case 0x0000:
            mes_tbl[MES_OFS_WELL_STATE] &= (~0x0001);    //油井在停止状态
            break;
        case 0x0001:
            mes_tbl[MES_OFS_WELL_STATE] |= 0x0001;    //油井在运行状态
            break;
        case 0x0002:
            mes_tbl[MES_OFS_WELL_STATE] &= (~0x0001);    //油井在停止状态
            break;
        case 0x0003:
            break;
        default:
            break;
    }
    
    if((mes_tbl[MES_OFS_DI_STATE] & 0x0004) == 0x0004){      
	mes_tbl[MES_OFS_WELL_STATE] &= (~0x0004);   //本地状态
    }else{
        mes_tbl[MES_OFS_WELL_STATE] |= 0x0004;      //远程状态
    }

    if((mes_tbl[MES_OFS_DI_STATE] & 0x0008) == 0x0008){
        mes_tbl[MES_OFS_WELL_STATE]  &= (~0x0080);      //工频状态
    }else{
	mes_tbl[MES_OFS_WELL_STATE] |= 0x0080;      //变频状态
        
    }
}

uint8_t get_di_state(uint8_t di_id){

	uint8_t i;
	uint8_t ret;
	uint8_t state = 0;
	uint16_t check_num = cfg_tbl[CFG_OFS_DI_CHECK_NUM];
	uint16_t check_tmr = cfg_tbl[CFG_OFS_DI_CHECK_TMR];
	OS_ERR os_err;

	if(check_num == 0){
		check_num = 40;
	}
	if(check_tmr == 0){
		check_tmr = 5;
	}

	for ( i = 0; i <check_num; i += 1 ) {
		switch(di_id){
			case 1:	
				ret = PLIB_PORTS_PinGet(PORTS_ID_0, PORT_CHANNEL_C, PORTS_BIT_POS_13);  //DI1
				break;
			case 2:
				ret = PLIB_PORTS_PinGet(PORTS_ID_0, PORT_CHANNEL_G, PORTS_BIT_POS_6);   //DI2
				break;
			case 3:
				ret = PLIB_PORTS_PinGet(PORTS_ID_0, PORT_CHANNEL_B, PORTS_BIT_POS_15);  //DI3
				break;
			case 4:
				ret = PLIB_PORTS_PinGet(PORTS_ID_0, PORT_CHANNEL_C, PORTS_BIT_POS_14);  //DI4
				break;
			default:	
				break;
		}				/* -----  end switch  ----- */
		if(i == 0){
			state = ret;
		}else{
			if(ret == state){

			}else{
				if (cfg_tbl[CFG_OFS_CAN_DEBUG_LEVEL] >= 2) {
					PLIB_PORTS_PinSet(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3);
					printf("DI<%d> Disturb\r\n", di_id);
					PLIB_PORTS_PinClear(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3);
				}
				return 2; /*未知状态*/ 
			}
		}
		OSTimeDly(check_tmr, OS_OPT_TIME_DLY, &os_err);
	}
	return state;
}

/*******************************************************************************
  Function:
    void APP1_Initialize ( void )

  Remarks:
    See prototype in app1.h.
 */

void APP1_Initialize ( void )
{
    /* Place the App state machine in its initial state. */
    app1Data.state = APP1_STATE_INIT;
    
    /* TODO: Initialize your application's state machine and other
     * parameters.
     */
}


/******************************************************************************
  Function:
    void APP1_Tasks ( void )

  Remarks:
    See prototype in app1.h.
 */

void APP1_Tasks(void)
{
	uint8_t ret;

	/* Check the application's current state. */
	switch (app1Data.state) {
		/* Application's initial state. */
	case APP1_STATE_INIT:
	{
		ret = get_di_state(1);
		switch(ret){
			case true:	
				if ((mes_tbl[MES_OFS_DI_STATE] & 0x0001) == 0x0001) {
					pcf8575_led_off(LED_HI_01);
					mes_tbl[MES_OFS_DI_STATE] &= (~0x0001);
					if (cfg_tbl[CFG_OFS_CAN_DEBUG_LEVEL] >= 2) {
						PLIB_PORTS_PinSet(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3);
						printf("DI1 OFF\r\n");
						PLIB_PORTS_PinClear(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3);
					}
				} else {

				}
				break;
			case false:	
				if ((mes_tbl[MES_OFS_DI_STATE] & 0x0001) == 0x0001) {

				} else {
					pcf8575_led_on(LED_HI_01);
					mes_tbl[MES_OFS_DI_STATE] |= 0x0001;
					if (cfg_tbl[CFG_OFS_CAN_DEBUG_LEVEL] >= 2) {
						PLIB_PORTS_PinSet(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3);
						printf("DI1 ON \r\n");
						PLIB_PORTS_PinClear(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3);
					}
				}
				break;
			default:	
				break;
		}				/* -----  end switch  ----- */
#if 0
		if (ret == true) {
			if ((mes_tbl[MES_OFS_DI_STATE] & 0x0001) == 0x0001) {
				pcf8575_led_off(LED_HI_01);
				mes_tbl[MES_OFS_DI_STATE] &= (~0x0001);
				if (cfg_tbl[CFG_OFS_CAN_DEBUG_LEVEL] >= 2) {
					PLIB_PORTS_PinSet(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3);
					printf("DI1 OFF\r\n");
					PLIB_PORTS_PinClear(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3);
				}
			} else {

			}
		} else {
			if ((mes_tbl[MES_OFS_DI_STATE] & 0x0001) == 0x0001) {

			} else {
				pcf8575_led_on(LED_HI_01);
				mes_tbl[MES_OFS_DI_STATE] |= 0x0001;
				if (cfg_tbl[CFG_OFS_CAN_DEBUG_LEVEL] >= 2) {
					PLIB_PORTS_PinSet(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3);
					printf("DI1 ON \r\n");
					PLIB_PORTS_PinClear(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3);
				}
			}
		}
#endif

		ret = get_di_state(2);
		switch(ret){
			case true:	
				if ((mes_tbl[MES_OFS_DI_STATE] & 0x0002) == 0x0002) {
					pcf8575_led_off(LED_HI_02);
					mes_tbl[MES_OFS_DI_STATE] &= (~0x0002);
					if (cfg_tbl[CFG_OFS_CAN_DEBUG_LEVEL] >= 2) {
						PLIB_PORTS_PinSet(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3);
						printf("DI2 OFF\r\n");
						PLIB_PORTS_PinClear(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3);
					}
				} else {

				}
				break;
			case false:	
				if ((mes_tbl[MES_OFS_DI_STATE] & 0x0002) == 0x0002) {

				} else {
					pcf8575_led_on(LED_HI_02);
					mes_tbl[MES_OFS_DI_STATE] |= 0x0002;
					if (cfg_tbl[CFG_OFS_CAN_DEBUG_LEVEL] >= 2) {
						PLIB_PORTS_PinSet(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3);
						printf("DI2 ON \r\n");
						PLIB_PORTS_PinClear(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3);
					}
				}
				break;
			default:	
				break;
		}				/* -----  end switch  ----- */

#if 0
		if (ret == true) {
			if ((mes_tbl[MES_OFS_DI_STATE] & 0x0002) == 0x0002) {
				pcf8575_led_off(LED_HI_02);
				mes_tbl[MES_OFS_DI_STATE] &= (~0x0002);
				if (cfg_tbl[CFG_OFS_CAN_DEBUG_LEVEL] >= 2) {
					PLIB_PORTS_PinSet(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3);
					printf("DI2 OFF\r\n");
					PLIB_PORTS_PinClear(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3);
				}
			} else {

			}
		} else {
			if ((mes_tbl[MES_OFS_DI_STATE] & 0x0002) == 0x0002) {

			} else {
				pcf8575_led_on(LED_HI_02);
				mes_tbl[MES_OFS_DI_STATE] |= 0x0002;
				if (cfg_tbl[CFG_OFS_CAN_DEBUG_LEVEL] >= 2) {
					PLIB_PORTS_PinSet(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3);
					printf("DI2 ON \r\n");
					PLIB_PORTS_PinClear(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3);
				}
			}
		}
#endif

		ret = get_di_state(3);
		switch(ret){
			case true:	
				if ((mes_tbl[MES_OFS_DI_STATE] & 0x0004) == 0x0004) {
					pcf8575_led_off(LED_HI_03);
					mes_tbl[MES_OFS_DI_STATE] &= (~0x0004);
					if (cfg_tbl[CFG_OFS_CAN_DEBUG_LEVEL] >= 2) {
						PLIB_PORTS_PinSet(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3);
						printf("DI3 OFF\r\n");
						PLIB_PORTS_PinClear(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3);
					}
				} else {

				}
				break;
			case false:	
				if ((mes_tbl[MES_OFS_DI_STATE] & 0x0004) == 0x0004) {

				} else {
					pcf8575_led_on(LED_HI_03);
					mes_tbl[MES_OFS_DI_STATE] |= 0x0004;
					if (cfg_tbl[CFG_OFS_CAN_DEBUG_LEVEL] >= 2) {
						PLIB_PORTS_PinSet(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3);
						printf("DI3 ON \r\n");
						PLIB_PORTS_PinClear(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3);
					}
				}
				break;
			default:	
				break;
		}				/* -----  end switch  ----- */

#if 0
		if (ret == true) {
			if ((mes_tbl[MES_OFS_DI_STATE] & 0x0004) == 0x0004) {
				pcf8575_led_off(LED_HI_03);
				mes_tbl[MES_OFS_DI_STATE] &= (~0x0004);
				if (cfg_tbl[CFG_OFS_CAN_DEBUG_LEVEL] >= 2) {
					PLIB_PORTS_PinSet(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3);
					printf("DI3 OFF\r\n");
					PLIB_PORTS_PinClear(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3);
				}
			} else {

			}
		} else {
			if ((mes_tbl[MES_OFS_DI_STATE] & 0x0004) == 0x0004) {

			} else {
				pcf8575_led_on(LED_HI_03);
				mes_tbl[MES_OFS_DI_STATE] |= 0x0004;
				if (cfg_tbl[CFG_OFS_CAN_DEBUG_LEVEL] >= 2) {
					PLIB_PORTS_PinSet(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3);
					printf("DI3 ON \r\n");
					PLIB_PORTS_PinClear(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3);
				}
			}
		}
#endif
		ret = get_di_state(4);
		switch(ret){
			case true:	
				if ((mes_tbl[MES_OFS_DI_STATE] & 0x0008) == 0x0008) {
					pcf8575_led_off(LED_HI_04);
					mes_tbl[MES_OFS_DI_STATE] &= (~0x0008);
					if (cfg_tbl[CFG_OFS_CAN_DEBUG_LEVEL] >= 2) {
						PLIB_PORTS_PinSet(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3);
						printf("DI4 OFF\r\n");
						PLIB_PORTS_PinClear(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3);
					}
				} else {

				}
				break;
			case false:	
				if ((mes_tbl[MES_OFS_DI_STATE] & 0x0008) == 0x0008) {

				} else {
					pcf8575_led_on(LED_HI_04);
					mes_tbl[MES_OFS_DI_STATE] |= 0x0008;
					if (cfg_tbl[CFG_OFS_CAN_DEBUG_LEVEL] >= 2) {
						PLIB_PORTS_PinSet(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3);
						printf("DI4 ON \r\n");
						PLIB_PORTS_PinClear(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3);
					}
				}
				break;
			default:	
				break;
		}				/* -----  end switch  ----- */
		get_well_state();

#if 0
		if (ret == true) {
			if ((mes_tbl[MES_OFS_DI_STATE] & 0x0008) == 0x0008) {
				pcf8575_led_off(LED_HI_04);
				mes_tbl[MES_OFS_DI_STATE] &= (~0x0008);
				if (cfg_tbl[CFG_OFS_CAN_DEBUG_LEVEL] >= 2) {
					PLIB_PORTS_PinSet(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3);
					printf("DI4 OFF\r\n");
					PLIB_PORTS_PinClear(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3);
				}
			} else {

			}
		} else {
			if ((mes_tbl[MES_OFS_DI_STATE] & 0x0008) == 0x0008) {

			} else {
				pcf8575_led_on(LED_HI_04);
				mes_tbl[MES_OFS_DI_STATE] |= 0x0008;
				if (cfg_tbl[CFG_OFS_CAN_DEBUG_LEVEL] >= 2) {
					PLIB_PORTS_PinSet(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3);
					printf("DI4 ON \r\n");
					PLIB_PORTS_PinClear(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3);
				}
			}
		}
#endif
		break;
	}

		/* TODO: implement your application state machine.*/

		/* The default state should never be executed. */
	default:
	{
		/* TODO: Handle error in application's state machine. */
		break;
	}
	}
}


/*******************************************************************************
 End of File
 */
